package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.tests.GroupData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase{
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		return getContacts(ENABLESORT);		
	}
	
	public SortedListOf<ContactData> getContacts(boolean doSort) {
		if (cachedContacts == null) {
			rebuildCache(doSort);
		}
		return cachedContacts;		
	}
	
	private void rebuildCache() {		
		rebuildCache(ENABLESORT);
	}
	
	private void rebuildCache(boolean doSort) {		
		boolean sortInEachIteration = false;
		manager.navigateTo().mainPage();
		
		cachedContacts = new SortedListOf<ContactData>();
		List<WebElement> rows = findElements(By.xpath(".//tr[@name='entry']"));
		for (int i = 0; i < rows.size(); i++) {
			if (i == rows.size()-1){
				sortInEachIteration = doSort;
			}
			WebElement row = rows.get(i);
			cachedContacts.add(new ContactData()
			.withFirstName(row.findElement(By.xpath("./td[3]")).getText())
			.withLastName(row.findElement(By.xpath("./td[2]")).getText())
			.withEmail(row.findElement(By.xpath("./td[4]/a")).getText())
			.withHome(row.findElement(By.xpath("./td[5]")).getText()), sortInEachIteration);
		}	
	}

	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		
		initContactCreation();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToHomePage(); 
		rebuildCache();
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().mainPage();
		
		initContactModification(index);
		fillContactForm(contact, ContactHelper.MODIFICATION);
		submitContactModification();
		returnToHomePage();	
		rebuildCache();
		return this;
	}

	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		
		initContactModification(index);
		submitContactDeletion();
		returnToHomePage();
		rebuildCache();
		return this;
	}

	public SortedListOf<GroupData> getGroupsFromAddingGrougList() {		
		return getGroupsFromAddingGrougList(ENABLESORT);
	}
	
	public SortedListOf<GroupData> getGroupsFromAddingGrougList(boolean doSort) {		
		manager.navigateTo().mainPage();
		
		SortedListOf<GroupData> groups = new SortedListOf<GroupData>();		
		List<WebElement> colls = findElements(By.xpath("//.[@name='to_group']/option"));
		for (WebElement coll: colls) {
			int i = 1;
			groups.add(new GroupData()
			.withName(coll.findElement(By.xpath(".//.[" + i + "]")).getText()));
			i++;
		}
		return groups;
	}
	
	//-------------------------------------------------------------

	private void submitContactDeletion() {
		click(By.xpath("//input[@value='Delete']"));
		cachedContacts = null;
	}	
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
	    type(By.name("firstname"), contact.getFirstName());
	    type(By.name("lastname"), contact.getLastName());
	    type(By.name("address"), contact.getAddress());
	    type(By.name("home"), contact.getHome());
	    type(By.name("mobile"), contact.getMobile());
	    type(By.name("work"), contact.getWork());
	    type(By.name("email"), contact.getEmail());
	    type(By.name("email2"), contact.getEmail2());
	    selectByText(By.name("bday"), contact.getBday());
	    selectByText(By.name("bmonth"), contact.getBmonth());	    	
	    type(By.name("byear"), contact.getByear());
	    if (formType == CREATION){
	    	selectByText(By.name("new_group"), contact.getGroup());
	    }	
	    type(By.name("address2"), contact.getAddress2());
	    type(By.name("phone2"), contact.getPhone2());
	    return this;
	}
	
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}
	
	public ContactHelper initContactModification(int index) {
		click(By.xpath("(//img[@title='Edit'])[" + (index+1) + "]"));	
		return this;	
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@value='Update']"));	
		cachedContacts = null;
		return this;	
	}
}
